package com.example.lanouhn.bantangapp.activities.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.lanouhn.bantangapp.R;
import com.example.lanouhn.bantangapp.activities.adapters.Adapter_FCommDyDt;
import com.example.lanouhn.bantangapp.activities.adapters.Adapter_FCommDy_Original;
import com.example.lanouhn.bantangapp.activities.contants.DT;
import com.example.lanouhn.bantangapp.activities.contants.DT_Image;
import com.example.lanouhn.bantangapp.activities.contants.DT_Original;
import com.example.lanouhn.bantangapp.activities.contants.DT_Original_Image;
import com.example.lanouhn.bantangapp.activities.utils.HttpUtils;
import com.example.lanouhn.bantangapp.activities.utils.JsonUtils_DT;
import com.example.lanouhn.bantangapp.activities.utils.JsonUtils_DT_Original;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanouhn on 16/7/23.
 */
public class CommunityArticalOriginal extends Activity {
    private ImageView back;//返回
    private XRecyclerView xRecyclerView;
    private Adapter_FCommDy_Original adapter;
    private List<DT_Original> dataList;//文本
    //判断网络是否连接
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:

                    adapter= new Adapter_FCommDy_Original(CommunityArticalOriginal.this,dataList);
                    xRecyclerView.setAdapter(adapter);


                    break;
                case 0:
                    Toast.makeText(CommunityArticalOriginal.this,R.string.fail,Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    //消除下拉刷新
                    xRecyclerView.refreshComplete();
                    break;

            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initview();
        initLinshener();
        initData();
    }


    //找到控件
    private void initview() {
        setContentView(R.layout.community_article_original_activity);
        back= (ImageView) findViewById(R.id.back);
        xRecyclerView= (XRecyclerView) findViewById(R.id.xrecyclerview);
        xRecyclerView.setLayoutManager(new LinearLayoutManager(getParent()));
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(1500);
                            initData();
                            handler.sendEmptyMessage(2);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }

            @Override
            public void onLoadMore() {

            }
        });
    }
//点击监听
    private void initLinshener() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }


    //绑定数据
    private void initData() {

        dataList = new ArrayList<>();
        new Thread(new Runnable() {
            @Override
            public void run() {
                String result = HttpUtils.doGet(DT_Original_Image.DT_LIST_URL);
                dataList = JsonUtils_DT_Original.getdtList(result);
                if (dataList.size() > 0) {
                    Message msg = new Message();
                    handler.sendEmptyMessage(1);

                } else {
                    handler.sendEmptyMessage(0);
                }
            }
        }).start();
    }

}
